package com.liuxvru.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuxvru.common.constant.ProjectAttachmentConstants;
import com.liuxvru.common.utils.SecurityUtils;
import com.liuxvru.project.domain.Project;
import com.liuxvru.project.domain.ProjectAttachment;
import com.liuxvru.project.mapper.ProjectAttachmentMapper;
import com.liuxvru.project.mapper.ProjectMapper;
import com.liuxvru.project.service.IProjectAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目附件Service业务层处理
 *
 * @author liuxvru
 * @date 2024-11-22
 */
@Service
public class ProjectAttachmentServiceImpl extends ServiceImpl<ProjectAttachmentMapper, ProjectAttachment> implements IProjectAttachmentService {
    @Autowired
    ProjectMapper projectMapper;

    /**
     * 查询项目附件列表
     *
     * @param projectAttachment 项目附件
     * @return 项目附件
     */
    @Override
    public List<ProjectAttachment> selectProjectAttachmentList(ProjectAttachment projectAttachment) {
        return baseMapper.selectProjectAttachmentList(projectAttachment);
    }

    @Override
    public List<ProjectAttachment> listByProjectId(Long projectId) {
        return baseMapper.selectList(new LambdaQueryWrapper<>(ProjectAttachment.class).eq(ProjectAttachment::getProjectId, projectId));
    }

    @Override
    public int add(Long projectId, ProjectAttachment projectAttachment) {
        Project project = projectMapper.selectById(projectId);
        if(project == null) throw new RuntimeException("项目不存在");
        Long userId = SecurityUtils.getUserId();
        projectAttachment.setCreatedBy(userId);
        projectAttachment.setUpdatedBy(userId);
        projectAttachment.setProjectId(projectId);
        projectAttachment.setReviewedBy(project.getLeaderId());
        projectAttachment.setReviewStatus(ProjectAttachmentConstants.PROJECT_ATTACHMENT_UNAUDITED);
        return baseMapper.insert(projectAttachment);
    }

    @Override
    public int edit(ProjectAttachment projectAttachment) {
        Long userId = SecurityUtils.getUserId();
        projectAttachment.setUpdatedBy(userId);
        return baseMapper.updateById(projectAttachment);
    }

}
